<template>
  <div class="login-container">
    <div class="login-box">
      <img
        src="http://likede2-admin.itheima.net/img/logo.595745bd.png"
        alt=""
      />
      <el-form
        code
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <!-- <h3 class="title">Login Form</h3> -->
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="请输入账号"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-form-item prop="yanzhengma">
          <div>
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="yanzhengma"
              v-model="loginForm.yanzhengma"
              placeholder="请输入验证码"
              name="yanzhengma"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="loginInput"
              style="width: 60.8333333333%"
            />
            <div class="login-img" @click="loginImg">
              <img
                ref="img"
                :src="codeImg"
                alt=""
                style="width: 100%; height: 52px"
              />
            </div>
          </div>
        </el-form-item>

        <el-button
          style="width: 100%; margin-bottom: 30px; background-color: #6b7def"
          @click="loginInput"
          v-loading="loading"
          >Login</el-button
        >
        <!-- <div class="tips">
          <span style="margin-right: 20px">username: admin</span>
          <span> password: any</span>
        </div> -->
      </el-form>
    </div>
  </div>
</template>

<script>
import { getUserImageCodeAPI, getUserLoginAPI } from "@/api";
export default {
  name: "Login",
  data() {
    const validateUsername = (rules, value, callback) => {
      if (!value) {
        callback(new Error("请输入正确的账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rules, value, callback) => {
      if (!value) {
        callback(new Error("请输入正确的密码"));
      } else {
        callback();
      }
    };
    return {
      codeImg: "",
      loginForm: {
        username: "admin",
        password: "admin",
        yanzhengma: "",
      },
      loginRules: {
        username: [
          {
            required: true,
            message: "请输入账号",
            trigger: "blur",
            validator: validateUsername,
          },
          { min: 4, max: 10, message: "请输入4-10位的账号", trigger: "blur" },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
            validator: validatePassword,
          },
          { min: 5, max: 16, message: "请输入6-16位的密码", trigget: "blur" },
        ],
        yanzhengma: [
          { required: true, message: "验证码输入不正确", trigger: "blur" },
        ],
      },
      loading: false,
      passwordType: "password",
      redirect: undefined,
      clientToken: "",
      loginType: 0,
      data: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    this.loginImg();
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    async loginImg() {
      this.clientToken = `${Math.ceil(Math.random() * 9)}`;

      const { data } = await getUserImageCodeAPI(this.clientToken);
      this.codeImg = window.URL.createObjectURL(data);
    },
    async loginInput() {
      this.data = {
        loginName: this.loginForm.username,
        password: this.loginForm.password,
        mobile: this.loginForm.username,
        code: this.loginForm.yanzhengma,
        clientToken: this.clientToken,
        loginType: 0,
        account: this.loginForm.username,
      };
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 调接口
          this.loading = true;
          this.$store
            .dispatch("user/getUserToken", this.data)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
        // if (valid) {
        //   this.$store
        //     .dispatch("user/login", this.loginForm)
        //     .then(() => {
        //       this.$router.push({ path: this.redirect || "/" });
        //       this.loading = false;
        //     })
        //     .catch(() => {
        //       this.loading = false;
        //     });
        // } else {
        //   console.log("error submit!!");
        //   return false;
        // }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      height: 47px;
      background: transparent;
      border: 0;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: #999;
      caret-color: #999;
      -webkit-appearance: none;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    width: 100%;
    height: 52px;
    margin-bottom: 24px;
    background: #fff;
    border: 1px solid #e2e2e2;
    border-radius: 4px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  background-image: url(../../assets/872cb363ac0759f1331aa48e2312be8.jpg);
  position: relative;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background-repeat: no-repeat;
  background-size: cover;

  .login-box {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
    img {
      position: absolute;
      width: 96px;
      height: 96px;
      top: -46px;
      left: 50%;
      margin-left: -48px;
    }
    .login-form {
    }
    .login-img {
      width: 29.1666666667%;
      height: 52px;
      position: relative;
      right: -300px;
      top: -8px;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
